package at.fhj.itm;
// TODO Add JavaDoc Documentation Comments

/**
 * 
 * @author Mike
 * @version 2 - Revision 44
 * 
 * this class shows the functionality of a queue list.
 * the first items will be worked out at first. 
 */

public class Queue {
	
	/**
	 * defining the variables
	 */
  private int maxSize;
  private long[] queArray;
  private int front;
  private int rear;
  private int nItems;

  /**
   * constructor of this class
   * initiates the defined variables
   * @param s is the size of the queue
   */
  public Queue(int s) {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
  }

  /**
   * puts an item at the end of a queue 
   * @param j is the number which will be added
   * @throws  ArrayIndexOutOfBoundsException if queue is full
   */
  public void insert(long j) {
	  if( isFull() ){
		  throw new ArrayIndexOutOfBoundsException("queue is full");
	  }
    if (rear == maxSize - 1) // deal with wraparound
      rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; 
  }

  /**
   * removes an item from the queue from the first position
   * @return gives back the value
   * @throws  ArrayIndexOutOfBoundsException if queue is empty
   */
  public long remove() {
	  if( isEmpty() ){
		  throw new ArrayIndexOutOfBoundsException("queue is empty");
	  }
    long temp = queArray[front++]; // get value and incr front
    if (front == maxSize) // deal with wraparound
      front = 0;
    nItems--; // one less item
    return temp;
  }

  /**
   * gives back the first item at the front(first) position
   * @return the first item
   */
  public long peekFront() {
    return queArray[front];
  }

  /**
   * checks if the Queue is empty or not
   * @return true if the queue is empty, false if empty
   */
  public boolean isEmpty() {
    return (nItems == 0);
  }

  /**
   * checks if the Queue is full
   * @return
   */
  public boolean isFull() {
    return (nItems >= maxSize);
  }

  /**
   * gives back the size of the queue
   * @return the size of queue
   */
  public int size() {
    return nItems;
  }

  // TODO Refactor / Delete Main Method and create JUnit test code
  public static void main(String[] args) {
    Queue theQueue = new Queue(5); // queue holds 5 items

    theQueue.insert(10);
    theQueue.insert(20);
    theQueue.insert(30);
    theQueue.insert(40);
    theQueue.insert(50);
    
    theQueue.remove(); 
    theQueue.remove(); 
    theQueue.remove();
    
    theQueue.insert(50); 
    theQueue.insert(60); //    (wraps around)
    theQueue.insert(70);

    while (!theQueue.isEmpty()) {
      long n = theQueue.remove(); // (40, 50, 60, 70, 80)
      System.out.print(n);
      System.out.print(" ");
    }
    System.out.println("");
  }
}
           
         